<template>
  <div v-loading="loading" class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="批次号" prop="orderId">
        <el-input
          v-model="queryParams.task_no"
          placeholder="请输入批次号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="海关仓库" prop="warehouse">
        <el-select
          v-model="queryParams.warehouse"
          placeholder="海关仓库"
          clearable
          size="small"
          style="width: 140px"
        >
          <el-option
            v-for="dict in warehouseOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="批次状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="批次状态"
          clearable
          size="small"
          style="width: 140px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="datePick"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleCreate"
        >创建批次</el-button>
      </el-col>
    </el-row>

    <el-table :data="list" style="width: 100%;">
      <el-table-column label="批次号" align="center" prop="task_no" />
      <el-table-column label="海关仓库" align="center" :formatter="warehouseFormat" />
      <el-table-column label="批次状态" align="center" :formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" width="180">
        <template slot-scope="scope">
          <span>{{

            parseTime(scope.row.time)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="openDetail(scope.row.task_no)"
          >订单操作</el-button>
          <el-button
            size="mini"
            type="success"
            :disabled="scope.row.status === 0 ? false : true"
            @click="finishTask(scope.row.task_no)"
          >完成</el-button>
          <el-button
            size="mini"
            type="danger"
            :disabled="scope.row.status === 0 ? false : true"
            @click="cancelTask(scope.row.task_no)"
          >撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />

    <!-- 修改进度 -->
    <el-dialog title="创建批次" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="addInfo" label-width="100px">
        <el-form-item label="请选择仓库" prop="status">
          <el-select v-model="addInfo.warehouse" placeholder="请选择仓库" :style="{width: '60%'}">
            <el-option
              v-for="(item, index) in warehouseOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addTask">提 交</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { taskList, modifyInfo, addTask, finishTask, cancelTask } from '@/api/logistics'
export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      open: false,
      row: undefined,
      addInfo: {},
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      statusOptions: [
        { label: '未完成', value: 0 },
        { label: '已完成', value: 1 }
      ],
      warehouseOptions: [
        { label: '龙潭香港仓库1', value: 'ltxgck1' },
        { label: '南京保税区仓库', value: 'njbsqck' },
        { label: '龙潭宁波保税区仓库', value: 'ningboLT' },
        { label: '泰州保税仓', value: 'taizhoubsq' }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        status: undefined,
        warehouse: undefined,
        task_no: undefined,
        start_time: undefined,
        end_time: undefined
      },
      ids: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true
      taskList(this.queryParams).then(response => {
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.status
      for (const d of this.statusOptions) {
        if (row.status === d.value) {
          status = d.label
          break
        }
      }
      return status
    },
    /** 仓库字典翻译 */
    warehouseFormat(row, column) {
      let warehouse = row.warehouse
      for (const d of this.warehouseOptions) {
        if (row.warehouse === d.value) {
          warehouse = d.label
          break
        }
      }
      return warehouse
    },
    /** 打开 */
    handleCreate(row) {
      this.addInfo = {}
      this.open = true
    },
    addTask() {
      console.log(JSON.stringify(this.progressInfo))
      if (this.addInfo.warehouse === undefined) {
        this.$message.error('请选择仓库')
        return
      }
      const that = this
      addTask(this.addInfo).then(response => {
        that.open = false
        that.$message.success(response.message)
        that.getList()
      })
    },
    /** 关闭 */
    cancel() {
      this.open = false
      this.addInfo = {}
    },
    /** 推送 */
    openDetail(id) {
      this.$router.push({
        path: '/logistics/shimei/task/detail',
        query: { id: id }
      })
    },
    /** 更新多单信息 */
    modifyInfoAll() {
      const that = this

      if (that.ids.length === 0) {
        that.$message.error('请选择订单')
        return
      }
      // console.log(data)
      that.$confirm('是否更新' + that.ids[0] + '...等物流信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        modifyInfo({ orders: that.ids }).then(response => {
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          }
          that.loading = false
        })
      }).catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams = {
        page: 1,
        limit: 10,
        status: undefined,
        warehouse: undefined,
        task_no: undefined,
        start_time: undefined,
        end_time: undefined
      }
      this.handleQuery()
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.start_time = value[0]
        this.queryParams.end_time = value[1]
      } else {
        this.queryParams.start_time = undefined
        this.queryParams.end_time = undefined
      }
    },
    /** 完成批次 */
    finishTask(task_no) {
      const that = this
      // console.log(data)
      that.$confirm('是否完成批次' + task_no + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        finishTask({ task_nos: [task_no] }).then(response => {
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          }
          that.loading = false
        })
      }).catch(() => {})
    },
    /** 完成批次 */
    cancelTask(task_no) {
      const that = this
      // console.log(data)
      that.$confirm('是否撤销批次' + task_no + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        cancelTask({ task_nos: [task_no] }).then(response => {
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          }
          that.loading = false
        })
      }).catch(() => {})
    },
    /** 禁止选中的项 */
    checkSelectable(row) {
      return row.status === 0
    },
    /** 选中的项 */
    handleSelectionChange(val) {
      const ids = []
      val.map((item, index, val) => {
        ids.push(item.task_no)
      })
      this.ids = ids
    }
  }
}
</script>
